package judesart.app.client.pub.home.ui;

import judesart.app.client.artwork.ArtworkController;
import judesart.app.client.artwork.ArtworkControllerAsync;
import judesart.app.client.pub.layout.ui.PublicMainLayout;
import judesart.app.domain.Artwork;
import judesart.app.domain.User;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HomePanel extends Composite {
    private static final int REFRESHING_INTERVAL = 3000;
    ArtworkControllerAsync artworkController = ArtworkController.Util.getInstance();
    private User user;
    private final Image image;
    private final VerticalPanel verticalPanel;
    private Timer timer;

    public HomePanel() {

        verticalPanel = new VerticalPanel();
        verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        verticalPanel.setSize("960px", "600px");
        verticalPanel.getElement().setId("id-jaw-HomePanel");

        HTML htmlwelcomeImJudith = new HTML("Hi, I'm Judith Shaylor. <span>This is my work.</span>", false);
        htmlwelcomeImJudith.setStyleName("jaw-home-welcome");
        verticalPanel.add(htmlwelcomeImJudith);
        verticalPanel.setCellHeight(htmlwelcomeImJudith, "40px");
        verticalPanel.setCellWidth(htmlwelcomeImJudith, "100%");
        verticalPanel.setCellVerticalAlignment(htmlwelcomeImJudith,
                HasVerticalAlignment.ALIGN_MIDDLE);
        verticalPanel.setCellHorizontalAlignment(htmlwelcomeImJudith,
                HasHorizontalAlignment.ALIGN_CENTER);
        htmlwelcomeImJudith.setSize("100%", "80px");

        HTML lblFeaturedWork = new HTML("featured work");
        lblFeaturedWork.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
        lblFeaturedWork.setWordWrap(false);
        lblFeaturedWork.setWidth("100%");
        lblFeaturedWork.setStyleName("jaw-home-feature");
        verticalPanel.add(lblFeaturedWork);

        lblFeaturedWork.setSize("100%", "100%");
        verticalPanel.setCellVerticalAlignment(lblFeaturedWork,
                HasVerticalAlignment.ALIGN_MIDDLE);

        image = new Image((String) null);
        verticalPanel.add(image);
        verticalPanel.setCellHeight(image, "400px");
        verticalPanel.setCellWidth(image, "800px");
        verticalPanel.setCellHorizontalAlignment(image,
                HasHorizontalAlignment.ALIGN_CENTER);
        initWidget(verticalPanel);
    }

    public void setUser(User user) {
        this.user = user;
        getRandomArtworkAsync();
        timer = new Timer() {
            @Override
            public void run() {
                getRandomArtworkAsync();
            }
        };
        timer.scheduleRepeating(REFRESHING_INTERVAL);
    }

    private void getRandomArtworkAsync() {
        artworkController.getRandomArtwork(
                PublicMainLayout.getInstance().getUser().getId(),
                new AsyncCallback<Artwork>() {

                    @Override
                    public void onSuccess(Artwork result) {
                        Log.debug("New artwork [" + result.getTitle() + "]");
                        image.setUrl("/image?id="
                                + result.getPhotos().get(0).getImageId()
                                + "&size=800x400");
                    }

                    @Override
                    public void onFailure(Throwable caught) {
                        Log.error("Unable to get random artwork", caught);
                    }
                });
    }

    public void stop() {
        if (timer != null) {
            timer.cancel();
        }
    }

    public void start() {
        if (timer != null) {
            timer.scheduleRepeating(REFRESHING_INTERVAL);
        }
    }
}
